<?php
namespace app\index\controller;

use think\Controller;
use think\Request;
use OSS\OssClient;
/*
 阿里云处理类
*/
//class Aliyun extends Common{
class Aliyun extends Controller{
	
	public function _initialize(){
	   parent::_initialize();
	}
	
    public function index(){
		$r = rand(100000,999999);
		return view('',['rand'=>$r]);
    }
	
	//删除图片，远程调用该
	private function delimg($remotePath){
		
		$remotePath = base64_decode($remotePath);
		
		$res = \ServerHelper::GetOSSConfig();
		$hp = new \AliyunOSSHelper($res);
		$data = $hp->DeleteImage($remotePath);
	
		print_r($data);
	}
	

	//前台客户上传，这个是
	public function frontupload(){
		//登陆身份验证
		
		//获取表单上传文件 例如上传了001.jpg
		$file = request()->file('image');
		if(is_null($file)){
			return ['flag' => false,'info'=>'请上传图片'];
		}

		$sunDir = date('Ym');
		$info = $file->validate(['size'=>10485760,'ext'=>'jpg,jpeg,png,gif'])->rule('uniqid')->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . $sunDir);

		if(!$info){
			$msg = $file->getError();
			return ['flag' => false,'info'=>$msg];
		}
		
		$url = 'uploads/' . $sunDir . '/' . str_replace(DS,'/',$info->getSaveName());
		
		$localPath = ROOT_PATH . 'public' . DS . $url;
		$localPath = str_replace('\\',DS,$localPath);
		$localPath = str_replace('/',DS,$localPath);
		
		$remotePath = $url;
		
		$res = \ServerHelper::GetOSSConfig();
		$hp = new \AliyunOSSHelper($res);
		$data = $hp->uploadLocal($localPath,$remotePath,true);
		return $data;
	}
	
	//通过网络请求参数执行上传
	public function uploadimage(){
		
		//登陆身份验证
		
		$localPath = Request::instance()->param('local_path');
        $localPath = str_replace(' ','+',$localPath);
        $___lo = '[before->'.$localPath.']';
        $localPath = base64_decode($localPath);
        $___lo.= '[after->'.$localPath.']';
        __log($___lo,'aliyunoss');
        
		if($localPath == ''){
			return ['flag'=>false,'info'=>'本地路径不能为空.'];
		}
		
		$remotePath = Request::instance()->param('remote_path','');
		if($remotePath == ''){
			//生成远程路径
			$extention = substr($localPath,strripos($localPath,'.'));
			$remotePath = 'uploads/' . date('Ym') . '/' . uniqid() .  $extention;
		}
		$dellocal = Request::instance()->param('dellocal',0,'intval');
		
		$res = \ServerHelper::GetOSSConfig();
		$hp = new \AliyunOSSHelper($res);
        $result = $hp->uploadLocal($localPath,$remotePath,$dellocal === 1);
		echo json_encode($result,JSON_UNESCAPED_UNICODE);
	}
	//生成OSS中的二维码链接
	public function qrcode(){
		
		$data = Request::instance()->param('data');
		if($data == ''){ ajax('empty string'); }
		
		$data = base64_decode($data);
		$savepath = RUNTIME_PATH . 'img' . DS . uniqid() . '_' . rand(1000,9999) . '.png';
		
		Vendor('phpqrcode.phpqrcode');
		\QRcode::png($data,$savepath);
		
		if(!file_exists($savepath)){
			ajax('生成失败');
		}
		
		//上传到OSS
		$remotePath = 'uploads/' . date('Ym') . '/' . uniqid() .  '.png';
		$res = \ServerHelper::GetOSSConfig();
		$hp = new \AliyunOSSHelper($res);
        $result = $hp->uploadLocal($savepath,$remotePath,true);
		echo json_encode($result,JSON_UNESCAPED_UNICODE);
	}

	
}
